<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.video_delete"
                   @click="handleDelete">删 除
        </el-button>
        <el-button size="small"
                   icon="el-icon-plus"
                   @click="videoNew"
                   plain>新 建
        </el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button type="text" size="small"
                   icon="el-icon-document-checked"
                   @click="showEditVideo(scope.row)">编辑</el-button>
        <el-button type="text" size="small"
                   icon="el-icon-document-delete"
                   v-if="scope.row.status === 0"
                   @click="setEffectStatus(scope.row,1)">禁用</el-button>
        <el-button type="text" size="small"
                   icon="el-icon-document-checked"
                   v-if="scope.row.status === 1"
                   @click="setEffectStatus(scope.row,0)">启用</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/videoanswer/video";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          menuWidth:170,
          tip: false,
          border: true,
          index: false,
          selection: true,
          columnBtn:false,
          refreshBtn:false,
          saveBtn:false,
          updateBtn:false,
          cancelBtn:false,
          addBtn:false,
          editBtn:false,
          viewBtn: false,
          page:false,

          column: [
            {
              label: "视频名称",
              width: 300,
              prop: "videoName",
              search: true
            },
            {
              label: "允许快进",
              prop: "isForward",
              type:"select",
              dicUrl: "/api/blade-system/dict/list/select?code=isForward",
              props: {
                label: "dictValue",
                value: "dictKey"
              }
            },
            {
              label: "红包类型",
              prop: "redEnvelopeType",
              type:"select",
              dicUrl: "/api/blade-system/dict/list/select?code=redEnvelopeType",
              props: {
                label: "dictValue",
                value: "dictKey"
              }
            },
            {
              label: "最低金额",
              prop: "redEnvelopeMin"
            },
            {
              label: "最高金额",
              prop: "redEnvelopeMax"
            },
            {
              label: "创建时间",
              width: 150,
              prop: "createTime"
            },
            {
              label: "状态",
              prop: "status",
              type:"select",
              search: true,
              dicUrl: "/api/blade-system/dict/list/select?code=status",
              props: {
                label: "dictValue",
                value: "dictKey"
              }
            }
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.video_add, false),
          viewBtn: this.vaildData(this.permission.video_view, false),
          delBtn: this.vaildData(this.permission.video_delete, false),
          editBtn: this.vaildData(this.permission.video_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowUpdate(row, index, loading, done) {
        update(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          done();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      videoNew(){
        this.$router.push({
          path: "/editVideo/index"
        });
      },
      showEditVideo(row){
        this.$router.push({
          path: "/editVideo/index",
          query:{row: row.id}
        });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params) {
        this.query = params;
        this.onLoad(this.page, params);
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
      //生效/失效 修改
      setEffectStatus(row,status){
        let updEffect = {id:row.id,status:status};
        update(updEffect).then(() => {
          row.status = status;
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          console.log(error);
        });
      }
    }
  };
</script>

<style>
</style>
